package com.peng.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.peng.dao.mapper.ArticleMapper;
import com.peng.dao.pojo.Article;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class ThreadService {
    @Async("asyncServiceExecutor")
    public void updateArticleViewCount(ArticleMapper articleMapper, Article article) {
        int viewCounts = article.getViewCounts();
        Article article1 = new Article();
        article1.setViewCounts(viewCounts+1);
        LambdaUpdateWrapper<Article> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(Article::getId,article.getId());
        wrapper.eq(Article::getViewCounts,viewCounts);
        articleMapper.update(article1,wrapper);
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
